import java.util.ArrayList;
import java.util.Scanner;


public class PriceMenuIterator implements MenuIterator{	
	
	private ArrayList<MenuItem> items = new ArrayList<MenuItem>();
	private int count;
	private double cost;
	private int current;
	Scanner in = new Scanner(System.in);
	public PriceMenuIterator(ArrayList<MenuItem> i, Double price)
	{
		items = i;
		count = 0;	
		//System.out.println("Enter maximum price:");
		//cost = in.nextDouble();
		cost = price;
	}

	@Override
	public MenuItem next() {
		
		setNext();
		return items.get(current);
	}
	/*
	 * hasNext() will check to see
	 * if there are more menu items in the array
	 * that are less than or equal to the specified price
	 */

	@Override
	public boolean hasNext() {
		boolean found = false;
		/*
		 * since we just want to know if there is a next item
		 * we will use a temporary count variable instead
		 * of updating our actual count variable 
		 */
		int tempCount = count;
		while(!found && tempCount < items.size())
		{
			if(items.get(tempCount) != null)		
			{
				if(items.get(tempCount).getPrice() >= cost)
				{
					tempCount++;
				}
				else
				{
					tempCount++;
					found = true;
				}
			}
		}
		return found;
		
	}
	
	/*
	 * setNext() sets the next item for the iterator to return
	 */
	private void setNext()
	{
		boolean found = false;
		
		while(!found && count < items.size())
		{
			if(items.get(count) != null)		
			{
				if(items.get(count).getPrice() >= cost)
				{
					count++;
				}
				else
				{
					found = true;
					current = count;
					count++;
				}
			}
		}
	}

}
